<?php

$loader->requireOnce('ordo/TicketItem.class.php');
$loader->requireOnce('ordo/Service.class.php');
$loader->requireOnce('ordo/QueueItem.class.php');
$loader->requireOnce('includes/PatientQueue/AbsQueue.class.php');
$loader->requireOnce('includes/AjaxRender/TicketRender.class.php');


class C_PaymentCollection extends Controller {
	var $template_mod;
	
	function C_PaymentCollection($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'Ticket') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'admin';
		}
		$this->template_mod = $template_mod;
		$this->view->path = 'payment_collection';
	}
		
	
	public function actionManagement() {
		// TODO: @LOCALIZATION
		
		$date = date('d/m/Y');
		$pieces = split('/', $date);
		$dateArr = array (
				'dd' => $pieces[0],
				'mm' => $pieces[1],
				'yyyy' => $pieces[2]
		);
		$this->assign('today', $dateArr);
		$orders	=	L10nHelper::getYMDorder();
        $this->assign('datePart_0', $orders[0]);
        $this->assign('datePart_1', $orders[1]);
        $this->assign('datePart_2', $orders[2]);
		return $this->view->render('payment_management.html');
	}
	
	public function actionGetAllRecords() {
		if (isset($_GET['date_dd']) && isset($_GET['date_mm']) && isset($_GET['date_yyyy'])) {
		    $date = $_GET['date_dd'] . "/" . $_GET['date_mm']. "/" . $_GET['date_yyyy'];
		} else {
			return false;
		}
		$obj = new DateObject();
		$dateObj = $obj->createFromVN($date);
		if ($dateObj->isValid()) {
			$date = $dateObj->toISO();
		} else {
			return false;
		}
		$p = Celini::newORDO('PaymentCollection');
		$result = $p->getPaymentRecords($date);
		$this->assign('result', $result);
		return $this->view->render('payment.html');
	}
	
	public function actionViewItemDetails() {
		$paymentColectionId = $_GET['payment_collection_id'];
		$p = Celini::newORDO('PaymentCollection', $paymentColectionId);
		$patientId = $p->get('patient_id');
		$ticketItemId = $p->get('ticket_item_id');
		// TODO: @LOCALIZATION
		$amount = $p->get('amount');
		
		$per = Celini::newORDO('Person', $patientId);
		$name = $per->get('name');
		$address = $per->getAddress();
		$code = Celini::newORDO('Patient', $patientId)->get('patient_code');
		$ticketId = Celini::newORDO('TicketItem', $ticketItemId)->get('ticket_id');
		$patientPayment = Celini::newORDO('Ticket', $ticketId)->get('patient_payment');
		
		$this->assign('name', $name);
		$this->assign('code', $code);
		$this->assign('note', $p->get('notes'));
		$this->assign('address', $address);
		$this->assign('realPayment', $amount);
		// TODO: @LOCALIZATION
		$paymentTime = $p->getFullDate($paymentColectionId);
		$this->assign('paymentTime', L10nHelper::renderDateTime($paymentTime,'short', 'datetime'));
		// TODO: @LOCALIZATION
		$this->assign('patientPayment', L10nHelper::renderNumber($patientPayment));
		$this->assign('paymentCollectionId', $paymentColectionId);
		return $this->view->render('payment_details.html');
	}
	
	public function actionViewMultiItemDetails() {
		$patientId = $_GET['patient_id'];
		$selectedDate = $_GET['selected_date'];
		
		$p = Celini::newORDO('PaymentCollection');
		$result = $p->getDuplicatedRecords($patientId, $selectedDate);
		
		$this->assign('result', $result);
		
		$per = Celini::newORDO('Person', $patientId);
		$patient = Celini::newORDO('Patient', $patientId);

		$this->assign('name', $per->get('name'));
		$this->assign('address', $per->getAddress());
		$this->assign('code', $patient->get('patient_code'));
		return $this->view->render('duplicated_payment.html');
		
	}
	
	public function actionSavePayment() {
		$paymentColectionId = $_POST['payment_collection_id'];
		// TODO: @LOCALIZATION
		$amount = $_POST['amount'];
		$note = $_POST['note'];
		
		$modifierId = $this->_me->get_person_id();
		if ($this->_me->get_username() == 'admin') {
			$modifierId = -1;
		}
		
		$p = Celini::newORDO('PaymentCollection');
		$p->updateRealAmount($amount, $paymentColectionId, $note);
		
		$p->setup($paymentColectionId);
		$ticketId = Celini::newORDO('TicketItem', $p->get('ticket_item_id'))->get('ticket_id');
		Celini::newORDO('Ticket')->updateRealAmount($amount, $ticketId, $modifierId);
	}
	
	
	

}
?>